/**
 * @Copyright ®2015 Sinosoft Co. Ltd. All rights reserved.<br/>
 * 项目名称 : 支付平台
 * 创建日期 : 2016年5月12日
 * 修改历史 : 
 *     1. [2016年5月12日]创建文件 by chenxin
 */
package com.sinosoft.sinopay.web.gateway.web.controller;

import com.alibaba.fastjson.JSON;
import com.sinosoft.sinocloud.common.biz.service.ReportLogService;
import com.sinosoft.sinocloud.common.config.ENUM_REPORTLOG_DEALTYPE;
import com.sinosoft.sinocloud.common.config.ENUM_REPORTLOG_REPORTTYPE;
import com.sinosoft.sinocloud.common.config.ENUM_REPORTLOG_STATE;
import com.sinosoft.sinocloud.common.dto.ReportLogDTO;
import com.sinosoft.sinocloud.platform.base.SinoBaseController;
import com.sinosoft.sinocloud.platform.common.exception.biz.BizDataErrException;
import com.sinosoft.sinocloud.platform.common.exception.biz.IllegalRequestException;
import com.sinosoft.sinocloud.platform.common.exception.biz.SinoBizException;
import com.sinosoft.sinocloud.platform.common.exception.checked.RepeatRequestException;
import com.sinosoft.sinocloud.platform.common.exception.system.SinoSystemException;
import com.sinosoft.sinocloud.platform.common.util.SecurityUtil;
import com.sinosoft.sinocloud.platform.common.util.StringUtil;
import com.sinosoft.sinopay.api.common.dto.pay.PaymentInfoDetailDTO;
import com.sinosoft.sinopay.merchants.dto.MerchantSignInfo;
import com.sinosoft.sinopay.merchants.facade.MerchantInfoFacadeService;
import com.sinosoft.sinopay.payrules.config.ENUM_PAY_TYPE;
import com.sinosoft.sinopay.payrules.dto.MerchantBankDTO;
import com.sinosoft.sinopay.payrules.dto.MerchantCheckDTO;
import com.sinosoft.sinopay.payrules.facade.PayRuleFacade;
import com.sinosoft.sinopay.payrules.facade.PayWayFacade;
import com.sinosoft.sinopay.paytrade.dto.PaymentConfirmDTO;
import com.sinosoft.sinopay.paytrade.dto.UserAuthConfInfo;
import com.sinosoft.sinopay.web.gateway.biz.config.ENUM_REPORT_BIZTYPE;
import com.sinosoft.sinopay.web.gateway.biz.config.GatewayPaymentConstants;
import com.sinosoft.sinopay.web.gateway.biz.config.GatewayView;
import com.sinosoft.sinopay.web.gateway.biz.service.GatewaySecurityService;
import com.sinosoft.sinopay.web.gateway.biz.service.PaymentGatewayService;
import com.sinosoft.sinopay.web.gateway.config.ENUM_PAY_RET;
import com.sinosoft.sinopay.web.gateway.config.ENUM_SMS_CONFIG;
import com.sinosoft.sinopay.web.gateway.dto.PayEntryRequestData;
import com.sinosoft.sinopay.web.gateway.dto.PayEntryResponseData;
import com.sinosoft.sinopay.web.gateway.dto.PaymentPageResponseData;
import com.sinosoft.sinopay.web.gateway.dto.SmsSendInfo;
import com.sinosoft.sinopay.web.util.HttpSmsUtil;

import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.*;

/**  
 * 【网关支付请求入口Controller】
 * @Description: 网关支付请求入口
 * @author 陈鑫
 * @date 2016年10月12日 下午6:01:49 
 * @version V1.0  
*/
@Controller
	@RequestMapping("/sinopay/getwayPay.do")
public class GetwayPayController extends SinoBaseController {
    @Autowired
    private PaymentGatewayService paymentGatewayService;
    /** 请求超时时间 */
    private Long overTime = 600000l;
    @Autowired
    private ReportLogService reportLogService;
    @Autowired
    private GatewaySecurityService securityService;
	@Autowired
	private PayWayFacade payWayFacade;
	@Autowired
	private PayRuleFacade payRuleFacade;
	@Autowired
	private MerchantInfoFacadeService merchantInfoFacadeService;

    /**
     * 获取网关支付许可
     * @param request 
     * @param response
     * @param model
     * @return
     */
    @RequestMapping(params = "action=gatewayRequest")
    public void getPayPermissionUrl(HttpServletRequest request, HttpServletResponse response, ModelMap model){
		PayEntryResponseData payEntryResponseData = new PayEntryResponseData();
		logger.info("网关支付开始");
		//1 获取请求参数
    	String tPayData = request.getParameter("pay_data");
    	String tSignMsg = request.getParameter("sign_msg");
    	String tRequestIp = getRemortIP(request);
    	ReportLogDTO tReportLogDTO = new ReportLogDTO();
    	try{
	    	//2.请求参数解密
	    	String tDecodeData = SecurityUtil.aesDecrypt(tPayData, GatewayPaymentConstants.REPORT_ENCRY_KEY);
	    	logger.info("请求参数为:"+tDecodeData);
	    	//3 解析报文
	    	PayEntryRequestData tPayEntryRequest = JSON.parseObject(tDecodeData,PayEntryRequestData.class);
	    	//4 验证请求合法性
	    	//4.1 验证签名是否合法
	    	logger.info("开始验证签名");
	    	String tMerchantCode = tPayEntryRequest.getMerchantCode();
			securityService.checkMerchantSignValid(tPayEntryRequest,tSignMsg);
	    	//4.2 验证是否为合法ip请求
			logger.info("开始验证IP");
			securityService.checkRequestIpValid(tRequestIp);
			//4.3 调用规则服务，进行支付规则校验
			MerchantCheckDTO merchantCheckDTO = new MerchantCheckDTO();
			merchantCheckDTO.setMerchantCode(tPayEntryRequest.getMerchantCode());
			merchantCheckDTO.setPayAmount(tPayEntryRequest.getPayAmount());
			merchantCheckDTO.setPayType(ENUM_PAY_TYPE.GATEWAY.code());//网关支付
			payRuleFacade.checkMerchant(merchantCheckDTO);

	    	//生成报文日志对象参数
	    	tReportLogDTO.setBusinessType(ENUM_REPORT_BIZTYPE.PAY_REQ.code());
	    	tReportLogDTO.setCallbackUrl("");
	    	tReportLogDTO.setDealType(ENUM_REPORTLOG_DEALTYPE.SYNC_DEAL.code());
	    	tReportLogDTO.setOrderBizNo(tPayEntryRequest.getBizNo());
	    	tReportLogDTO.setReportType(ENUM_REPORTLOG_REPORTTYPE.RECEIVE.code());
	    	tReportLogDTO.setRequestIp(tRequestIp);
	    	tReportLogDTO.setRequestXml(tDecodeData);
	    	tReportLogDTO.setSerialNo(tPayEntryRequest.getSerialNo());
	    	tReportLogDTO.setSourceCode(tPayEntryRequest.getMerchantCode());
	    	//5 验证是否为重复交易
	    	logger.info("开始验证重复交易");
	    	String tSerialNo = tPayEntryRequest.getSerialNo();
	    	String tPayAmountStr =  tPayEntryRequest.getPayAmount().setScale(2, BigDecimal.ROUND_HALF_DOWN).toString();
			securityService.checkRepeatRequest(tMerchantCode,tSerialNo);
	    	//6 调用服务，持久化支付请求数据，并获取合法支付url
			String tPayPageUrl = paymentGatewayService.dealGatewayPayRequest(tPayEntryRequest);
			//7 请求成功
			logger.info("返回支付地址："+tPayPageUrl);
			payEntryResponseData.setRetCode(ENUM_PAY_RET.PAY_RET_SUCCESS.code());
			payEntryResponseData.setRetMsg(ENUM_PAY_RET.PAY_RET_SUCCESS.desc());
			payEntryResponseData.setGateWayPayUrl(tPayPageUrl);

    	}
    	catch(BizDataErrException ex){
			logger.error(ex.getStackTrace());
			logger.error(ex.getMessage());
			payEntryResponseData.setRetCode(ENUM_PAY_RET.PAY_RET_FAIL.code());
			payEntryResponseData.setRetMsg(ex.getMessage());
			tReportLogDTO.setState(ENUM_REPORTLOG_STATE.EXCEPTION.code());
			tReportLogDTO.setDescription(ex.getMessage());
		}
    	catch(RepeatRequestException ex){
    		logger.error(ex.getStackTrace());
    		logger.error(ex.getMessage());
			payEntryResponseData.setRetCode(ENUM_PAY_RET.PAY_RET_FAIL.code());
			payEntryResponseData.setRetMsg(ex.getMessage());
	     	tReportLogDTO.setState(ENUM_REPORTLOG_STATE.EXCEPTION.code());
	     	tReportLogDTO.setDescription(ex.getMessage());
    	}
    	catch(IllegalRequestException ex){
    		logger.error(ex.getStackTrace());
    		logger.error(ex.getMessage());
			payEntryResponseData.setRetCode(ENUM_PAY_RET.PAY_RET_FAIL.code());
			payEntryResponseData.setRetMsg("非法支付请求！");
	     	tReportLogDTO.setState(ENUM_REPORTLOG_STATE.EXCEPTION.code());
	     	tReportLogDTO.setDescription(ex.getMessage());
    	}
    	catch(SinoBizException ex){
    		logger.error(ex.getStackTrace());
    		logger.error(ex.getMessage());
			payEntryResponseData.setRetCode(ENUM_PAY_RET.PAY_RET_FAIL.code());
			payEntryResponseData.setRetMsg(ex.getMessage());
	     	tReportLogDTO.setState(ENUM_REPORTLOG_STATE.EXCEPTION.code());
	     	tReportLogDTO.setDescription(ex.getMessage());
    	}
    	catch(SinoSystemException ex){
    		logger.error(ex.getStackTrace());
    		logger.error(ex.getMessage());
			payEntryResponseData.setRetCode(ENUM_PAY_RET.PAY_RET_FAIL.code());
			payEntryResponseData.setRetMsg("系统异常，请联系管理员！");
	     	tReportLogDTO.setState(ENUM_REPORTLOG_STATE.EXCEPTION.code());
	     	tReportLogDTO.setDescription(ex.getMessage());
    	}
    	catch(Exception ex){
			logger.error(ex.getStackTrace());
			logger.error(ex.getMessage());
			payEntryResponseData.setRetCode(ENUM_PAY_RET.PAY_RET_FAIL.code());
			payEntryResponseData.setRetMsg("系统未知异常，请联系管理员！");
	     	tReportLogDTO.setState(ENUM_REPORTLOG_STATE.EXCEPTION.code());
	     	tReportLogDTO.setDescription(ex.getMessage());
    	}
    	finally{
    		//8.持久化报文
	    	//reportLogService.logReport(tReportLogDTO);
			try {
				response.setContentType("text/html");
				response.setCharacterEncoding("UTF-8");
				response.getWriter().print(JSON.toJSONString(payEntryResponseData));
			} catch (IOException e) {
				logger.error(e.getStackTrace());
				logger.error(e.getMessage());
			}
    	}
    }


	/**
	 * 方法的功能描述：跳转支付网关页面
	 * @param request
	 * @param response
	 * @param model
	 * @return String
	 * @throw
	 * @author bianzb
	 * @version V1.0
	 * @date 2016-12-15
	 */
	@RequestMapping(params = "action=gateWayPay")
	public String gateWayPay(HttpServletRequest request, HttpServletResponse response, ModelMap model){
		//1 获取请求参数
		String tPayNo = request.getParameter("payNo");
		String tSignMsg = request.getParameter("signMsg");
		String tReturnView = GatewayView.PAY_MAIN_PAGE_VIEW;
		try{
			PaymentInfoDetailDTO tPayInfo = paymentGatewayService.queryPaymentInfoByPayNo(tPayNo);
			String tMerchantCode = tPayInfo.getMerchantCode();

			//2.验证签名和时间戳
			Map<String,String> tSignInfoMap = new HashMap<String,String>();
			tSignInfoMap.put("serialNo",tPayInfo.getSerialNo());//交易流水号
			tSignInfoMap.put("bizNo",tPayInfo.getBizNo());//业务号码
			tSignInfoMap.put("payAmount",tPayInfo.getPayAmount().setScale(2, BigDecimal.ROUND_HALF_DOWN).toString());//支付金额
			tSignInfoMap.put("merchantCode",tPayInfo.getMerchantCode());//商户编码
			tSignInfoMap.put("transDesc",tPayInfo.getTradeDesc());//交易描述
			String reqSignMsg = securityService.createSignMsg(tPayInfo.getMerchantCode(),tSignInfoMap);

			if(!reqSignMsg.equals(tSignMsg)){
				throw new IllegalRequestException("签名不合法");
			}

			Long tTimeStamp  = new Date().getTime();
			Long tTimeStamp1 = tPayInfo.getCreateDate().getTime();
			if(tTimeStamp - tTimeStamp1 > overTime){
				throw new SinoSystemException("支付超时");
			}

			//3. 调用规则服务，获取可支付银行列表
			List<MerchantBankDTO> merchantBankDTOList = payWayFacade.getMerchantBankList(tMerchantCode);

			//4. 获取支付页面信息
			Map tParamsMap = new HashMap<String,String>();
			tParamsMap.put("payNo",tPayNo);
			tParamsMap.put("merchantCode",tMerchantCode);
			tParamsMap.put("payAmount",tPayInfo.getPayAmount().setScale(2, BigDecimal.ROUND_HALF_DOWN).toString());
			String signMsg = securityService.createSignMsg(tMerchantCode , tParamsMap);
			request.getSession().setAttribute(signMsg,new Date().getTime());
			model.put("sign", signMsg);
			model.put(GatewayPaymentConstants.PAY_NO , tPayNo);
			model.put(GatewayPaymentConstants.SERIAL_NO , tPayInfo.getSerialNo());
			model.put(GatewayPaymentConstants.ACCOUNT_NAME, tPayInfo.getAccountName());
			model.put(GatewayPaymentConstants.ACCOUNT_NO, tPayInfo.getAccountNo());
			model.put(GatewayPaymentConstants.BANK_CODE, tPayInfo.getBankCode());
			model.put(GatewayPaymentConstants.BANK_NAME, tPayInfo.getBankName());
			model.put(GatewayPaymentConstants.CARD_NO, tPayInfo.getIdNo());
			model.put(GatewayPaymentConstants.CARD_TYPE, tPayInfo.getIdType());
			model.put(GatewayPaymentConstants.PAY_AMOUNT,tPayInfo.getPayAmount());
			model.put(GatewayPaymentConstants.MERCHANT_CODE, tMerchantCode);
			model.put(GatewayPaymentConstants.TRADE_DESC, tPayInfo.getTradeDesc());
			model.put(GatewayPaymentConstants.MOBILE, tPayInfo.getMobile());
			model.put("merchantBankDTOList",merchantBankDTOList);//银行列表信息
		}
		catch(BizDataErrException ex){
			logger.error(ex.getStackTrace());
			logger.error(ex.getMessage());
			model.put("error_msg", ex.getMessage());
			tReturnView = GatewayView.PAY_ERR_PAGE_VIEW;
		}
		catch(IllegalRequestException ex){
			logger.error(ex.getStackTrace());
			logger.error(ex.getMessage());
			model.put("error_msg", "非法支付请求！");
			tReturnView = GatewayView.PAY_ERR_PAGE_VIEW;
		}
		catch(SinoBizException ex){
			logger.error(ex.getStackTrace());
			logger.error(ex.getMessage());
			model.put("error_msg", ex.getMessage());
			tReturnView = GatewayView.PAY_ERR_PAGE_VIEW;
		}
		catch(SinoSystemException ex){
			logger.error(ex.getStackTrace());
			logger.error(ex.getMessage());
			model.put("error_msg", ex.getMessage());
			tReturnView = GatewayView.PAY_ERR_PAGE_VIEW;
		}
		catch(Exception ex){
			logger.error(ex.getStackTrace());
			logger.error(ex.getMessage());
			model.put("error_msg", "系统未知异常，请联系管理员！");
			tReturnView = GatewayView.PAY_ERR_PAGE_VIEW;
		}

		return tReturnView;
	}


    /**
     * 支付请求授权确认
     * @param request
     * @param response
     * @param model
     * @return
     */
	@RequestMapping(params = "action=confirmPayment",method=RequestMethod.POST)
    public void confirmPayment(HttpServletRequest request, HttpServletResponse response, ModelMap model){
		String tPayNo = request.getParameter("pay_no");
    	String tSign = request.getParameter("sign_msg");
		String tMerchantCode = request.getParameter("merchant_code");
		String tBankCode = request.getParameter("bank_code");
		String tCardNo = request.getParameter("card_no");
//		String tBankName = request.getParameter("bank_name");
		String tPayAmount = "";
		String tBizNo = "";
		PaymentPageResponseData paymentPageResponseData = new PaymentPageResponseData();
		UserAuthConfInfo userAuthConfInfo = null;
    	try{
    		logger.info("银行编码:"+tBankCode);
    		logger.info("银行账号:"+tCardNo);
//    		logger.info("银行名字:"+tBankName);
	    	//1.获取支付相关信息 
			PaymentInfoDetailDTO tPayInfo = paymentGatewayService.queryPaymentInfoByPayNo(tPayNo);
			tBizNo = tPayInfo.getBizNo();
	    	tPayAmount = tPayInfo.getPayAmount().setScale(2, BigDecimal.ROUND_HALF_DOWN).toString();
			Map tParamsMap = new HashMap<String,String>();
			tParamsMap.put("payNo",tPayNo);
			tParamsMap.put("merchantCode",tPayInfo.getMerchantCode());
			tParamsMap.put("payAmount",tPayAmount);
			//2.验证签名
			String signMsg = securityService.createSignMsg(tMerchantCode , tParamsMap);
			if(signMsg ==null || !signMsg.equals(tSign)){
				throw new IllegalRequestException("签名不合法");
			}
			//更新订单表
			PaymentConfirmDTO cPayConfirmInfo = new PaymentConfirmDTO();
			cPayConfirmInfo.setPayNo(tPayNo);
			cPayConfirmInfo.setBankCode(tBankCode);
			cPayConfirmInfo.setAccountNo(tCardNo);
//			cPayConfirmInfo.setBankName(tBankName);
			paymentGatewayService.modifyTradeOrderStatus(cPayConfirmInfo);
			logger.info("更新订单表结束");
			//3.验证签名和时间戳
			Long tTimeStamp  = new Date().getTime();
			Long tTimeStamp1 = (Long)request.getSession().getAttribute(tSign);
			if(tTimeStamp - tTimeStamp1 > overTime){
				throw new SinoSystemException("支付超时");
			}
			request.getSession().removeAttribute(tSign);

			//4.调用交易服务的交易提交
			logger.info("开始调用交易服务========交易提交");
			userAuthConfInfo = paymentGatewayService.confirmPayment(tPayNo);
			logger.info("回调地址为:"+userAuthConfInfo.getNotifyUrl());
			paymentPageResponseData.setRetCode(userAuthConfInfo.getReturnCode());
			paymentPageResponseData.setRetMsg(userAuthConfInfo.getReturnMsg());

    	}catch(IllegalRequestException ex){
			logger.error(ex.getStackTrace());
			paymentPageResponseData.setRetCode(ENUM_PAY_RET.PAY_RET_FAIL.code());
			paymentPageResponseData.setRetMsg(ex.getMessage());
		}
    	catch(BizDataErrException ex){
			logger.error(ex.getStackTrace());
			paymentPageResponseData.setRetCode(ENUM_PAY_RET.PAY_RET_FAIL.code());
			paymentPageResponseData.setRetMsg(ex.getMessage());
		}
		catch(SinoBizException ex){
			logger.error(ex.getStackTrace());
			paymentPageResponseData.setRetCode(ENUM_PAY_RET.PAY_RET_FAIL.code());
			paymentPageResponseData.setRetMsg(ex.getMessage());
		}
		catch(SinoSystemException ex){
			logger.error(ex.getStackTrace());
			paymentPageResponseData.setRetCode(ENUM_PAY_RET.PAY_RET_FAIL.code());
			paymentPageResponseData.setRetMsg( ex.getMessage());
		}
		catch(Exception ex){
			logger.error(ex.getStackTrace());
			paymentPageResponseData.setRetCode(ENUM_PAY_RET.PAY_RET_FAIL.code());
			paymentPageResponseData.setRetMsg( "系统未知异常，请联系管理员！");
		} finally {
			try {
				String notifyUrl = this.buildMerchantNotifyUrl(userAuthConfInfo, paymentPageResponseData,tPayNo,tPayAmount,tBizNo);
				logger.info("回调地址为："+notifyUrl);
				paymentPageResponseData.setSyncNotifyUrl(notifyUrl);
				response.getWriter().write(JSON.toJSONString(paymentPageResponseData));
			} catch (Exception e){
				e.printStackTrace();
			}
		}
    }

    /**
     * 方法的功能描述：构建商户回调URL
     * @param
     * @return
     * @throw
     * @author bianzb
     * @version V1.0
     * @date 2016-12-20
     */
    public static String buildMerchantNotifyUrl(UserAuthConfInfo userAuthConfInfo,PaymentPageResponseData paymentPageResponseData,String payNo,String payAmount,String bizNo){
//		StringBuffer paramBuf = new StringBuffer();
//		paramBuf.append("?payNo=" + payNo);
//		paramBuf.append("&bizNo="+bizNo);
//		paramBuf.append("&payAmount=" + payAmount);
//		paramBuf.append("&returnCode=" + paymentPageResponseData.getRetCode());
		Map<String,String> nMap = new HashMap<String,String>();
		nMap.put("payNo", payNo);
		nMap.put("bizNo", bizNo);
		nMap.put("payAmount", payAmount);
		nMap.put("returnCode", paymentPageResponseData.getRetCode());
		JSONObject jsonObject = JSONObject.fromObject(nMap);
		System.out.println("页面回调参数="+jsonObject);
		return userAuthConfInfo.getNotifyUrl() + "?data="+jsonObject.toString();
	}

    /**
     * 发送验证码
     * @param request
     * @param response
     * @param model
     */
	@RequestMapping(params = "action=sendYzm",method=RequestMethod.POST)
    public void sendYZM(HttpServletRequest request, HttpServletResponse response, ModelMap model){

		try {
			String tMobile = request.getParameter("mobile");
			String authCode = (int)(Math.random()*1000000) + "";
			final HttpSession httpSession = request.getSession();
			httpSession.setAttribute("authCode" ,authCode);
			String content = "验证码：" + authCode + ", 10分钟内输入有效。" ;
			SmsSendInfo smsSendInfo = new SmsSendInfo();
			smsSendInfo.setMobile(tMobile);
			smsSendInfo.setContent(content);
//			smsSendInfo.setType(ENUM_SMS_CONFIG.WEBSTITE);
			smsSendInfo.setType(ENUM_SMS_CONFIG.PAYUSER);
			HttpSmsUtil httpSmsUtil = new HttpSmsUtil();
			Boolean flag = httpSmsUtil.sendSms(smsSendInfo);
			final Timer timer = new Timer();
			timer.schedule(new TimerTask() {
				@Override
				public void run() {
					httpSession.removeAttribute("authCode");
					timer.cancel();
				}
			},10*60*1000);

			String resultMsg = "";
			if(flag){
				resultMsg = "{'success':'true'}";
			}else {
				resultMsg = "{'success':'false'}";
			}
			response.getWriter().write(resultMsg);
		} catch (Exception e) {
			logger.error(e.getStackTrace());
		}

    }

    /**
     * 方法的功能描述：手机验证码校验
     * @param
     * @return
     * @throw
     * @author bianzb
     * @version V1.0
     * @date 2016-12-22
     */
	@RequestMapping(params = "action=checkYzm",method=RequestMethod.POST)
    public void checkYZM(HttpServletRequest request, HttpServletResponse response, ModelMap model){
		String checkAuthCode = request.getParameter("auth_code");
		try {
			final HttpSession httpSession = request.getSession();
			String sessionAuthCode = (String)httpSession.getAttribute("authCode");
			String resultMsg = "";
			if(StringUtil.isNotBlank(sessionAuthCode)){
				if(sessionAuthCode.equals(checkAuthCode)){
					resultMsg = "{'success':'true'}";
				}else {
					resultMsg = "{'success':'false'}";
				}
			}else{
				resultMsg = "{'success':'false'}";
			}


			response.getWriter().write(resultMsg);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

    /**
     * 获取请求ip
     * @param request
     * @return
     */
	public String getRemortIP(HttpServletRequest request) {
		if (request.getHeader("x-forwarded-for") == null) {
			return request.getRemoteAddr();
		}
		return request.getHeader("x-forwarded-for");
	}


}
